package in.waffl.fairycake;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Applet extends JApplet {
	private static final long serialVersionUID = -857089780849409792L;
	private JPanel contentPane;
	private final Canvas canvas;
	public Applet() {
		this.setSize(640, 480);
		contentPane = new JPanel();
		contentPane.setBackground(Color.BLACK);
		contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		canvas = new Canvas();
		canvas.setBackground(Color.WHITE);
		contentPane.add(canvas, BorderLayout.CENTER);
		LogicExecutor logicExecutor = new LogicExecutor(1, canvas);
		Main.setLogicExecutor(logicExecutor);
		EventQueue.invokeLater(logicExecutor);
		new Thread(new Runnable() {
			public void run() {
				while (!canvas.isVisible()) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				boolean success = false;
				while (!success) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					try {
						canvas.createBufferStrategy(2);
						success = true;
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				canvas.start();
			}
		}).start();
	}
}
